//
//  KSForgotPasswordViewController.swift
//  Merchant
//
//  Created by liuxm on 2017/8/8.
//  Copyright © 2017年 huanniu. All rights reserved.
//

import UIKit

/// 忘记密码
class KSForgotPasswordViewController: KSBaseViewController {
    
    @IBOutlet weak var phoneTextField: UITextField!
    @IBOutlet weak var codeTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var passwordAgainTextField: UITextField!
    @IBOutlet weak var getAuthCodeBtn: UIButton!
    @IBOutlet weak var affirmBtn: UIButton!
    
    var timerCountDown: Timer?
    var countDownSeconds: Int = 60
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "忘记密码"
        
        self.affirmBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorMainRed), for: UIControlState.normal)
        self.affirmBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorButtonHighlight), for: UIControlState.highlighted)
        self.affirmBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorButtonDisabled), for: UIControlState.disabled)
    }
    
    override func requestSuccess(result: String, apiType: KSApiType) {
        if apiType == KSApiType.KSSendCode {
            self.progressHUD.showMessage(text: "验证码已发送")
        } else {
            self.progressHUD.showMessage(text: "密码修改成功")
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    private func startTimer() {
        self.getAuthCodeBtn.isEnabled = false
        self.timerCountDown = Timer.init(timeInterval: 1, target: self, selector: #selector(timeFireMethod), userInfo: nil, repeats: true)
        RunLoop.main.add(self.timerCountDown!, forMode: RunLoopMode.commonModes)
        self.timerCountDown?.fire()
    }
    
    @objc private func timeFireMethod() {
        self.countDownSeconds -= 1
        self.getAuthCodeBtn.setTitle("\(self.countDownSeconds)秒后重发", for: UIControlState.normal)
        
        if self.countDownSeconds == 0 {
            self.getAuthCodeBtn.isEnabled = true
            self.getAuthCodeBtn.setTitle("获取验证码", for: UIControlState.normal)
            self.timerCountDown?.invalidate()
            self.timerCountDown = nil
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.phoneTextField.resignFirstResponder()
        self.codeTextField.resignFirstResponder()
        self.passwordTextField.resignFirstResponder()
        self.passwordAgainTextField.resignFirstResponder()
    }
    
    private func judgeKeyboardChange() {
        if (self.phoneTextField.text?.isEmpty)! || (self.codeTextField.text?.isEmpty)! || (self.passwordTextField.text?.isEmpty)! || (self.passwordAgainTextField.text?.isEmpty)! {
            self.affirmBtn.isEnabled = false
        } else {
            self.affirmBtn.isEnabled = true
        }
    }
    
    @IBAction func phoneTextFieldChange(_ sender: UITextField) {
        self.getAuthCodeBtn.isEnabled = (sender.text?.isEmpty)! ? false : true
        self.judgeKeyboardChange()
    }
    
    @IBAction func codeTextFieldChange(_ sender: Any) {
        self.judgeKeyboardChange()
    }
    
    @IBAction func passwordTextFieldChange(_ sender: Any) {
        self.judgeKeyboardChange()
    }
    
    @IBAction func passwordAgainTxtFieldChange(_ sender: Any) {
        self.judgeKeyboardChange()
    }
    
    @IBAction func applyRights(_ sender: UIButton) {
        
    }
    
    @IBAction func phoneCall(_ sender: UIButton) {
        KSTools.telephone(phone: "0571-898806504")
    }
    
    @IBAction func showNewPwdEvent(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        self.passwordTextField.isSecureTextEntry = !sender.isSelected
    }
    
    @IBAction func showAgainPwdEvent(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        self.passwordAgainTextField.isSecureTextEntry = !sender.isSelected
    }
    
    @IBAction func affirmEvent(_ sender: Any) {
        if self.passwordAgainTextField.text != self.passwordTextField.text {
            self.progressHUD.showMessage(text: "请保证两次密码一致")
        } else if (self.passwordTextField.text?.characters.count)! < 6 && (self.passwordTextField.text?.characters.count)! > 20 {
            self.progressHUD.showMessage(text: "请输入6到20位的密码")
        } else {
            let parameters: [String: Any] = ["mobile" :self.phoneTextField.text!,"code" :self.codeTextField.text!,"new_password" :self.passwordTextField.text!, "confirm_password" :self.passwordAgainTextField.text!]
            self.request.requestForgetLoginPwd(parameters: parameters)
        }
    }
    
    @IBAction func getAuthCodeEvent(_ sender: Any) {
        if KSTools.checkPhoneNum(number: self.phoneTextField.text!) {
            self.startTimer()
            self.request.requestSendSMSLogin(mobile: self.phoneTextField.text!, tempId: "1")
        } else {
            self.progressHUD.showMessage(text: "请输入正确的手机号")
        }
    }
}

